{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "目标函数最大值: 812.9999732402549\n",
      "最优解 [ 9.89999977e+01  7.60795655e-07  3.33332599e-01  2.30736857e-06\n",
      " -9.99999896e+00]\n",
      "最优解 [ 99.     0.     0.33   0.   -10.  ]\n"
     ]
    }
   ],
   "source": [
    "#5.1\n",
    "\n",
    "#化为标准型\n",
    "from scipy.optimize import linprog\n",
    "import numpy as np\n",
    "c= [-8,2,-3,1,2]\n",
    "A= [[1,1,1,1,1],[1,2,2,1,6],[2,1,6,0,0],[0,0,1,1,5]]\n",
    "b= [[400],[800],[200],[200]]\n",
    "LB= [0,0,0,0,-10]\n",
    "UB =[99,99,99,99,None]\n",
    "bound = tuple(zip(LB,UB))\n",
    "res = linprog(c,A,b,None,None,bound)\n",
    "print('目标函数最大值:',-res.fun)\n",
    "print('最优解',res.x)\n",
    "print('最优解',np.round(res.x,decimals=2))#保留两位有效数字后的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 432x288 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAEGCAYAAABrQF4qAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAWEElEQVR4nO3df6zdd33f8efLdgxEMHAdpm3OD8fCIAKbSHIaXFXjhyCph6a4YtCYOFrYQqNRsknLNgmEBJWRqlBENaZFIp4b9YcKAVKps1pQykoYWsVNfS+sgFOlOLdxYrcSruMyaWZxbL/3xznHnFxf33vuveeeH9/zfEiWzznf77n38/G1X+ftz+fz/XxTVUiSmmvDqBsgSVpfBr0kNZxBL0kNZ9BLUsMZ9JLUcJtG3YCFrrrqqtq+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jbst6SrIB+A3ggyNuyihsoj188w7a/3P7VpJ/XFV/N8pGrbMPAL9VVZ9N8nO072r35qq6MOqGjbtJrOhXcrNyFtysfJL1daP1JO8GPg7cXlUvDKlt62W5Pr8KeDPwzSTP0B7DPNSACdl+ftbHgUNV9WJV/RXwl7SDf1L10+d7gC8DVNW3gZfT3viryfr6d7+cSQz6izcrT7KZ9mTroQXndG9WDj03Kx9iG9fDsv1OciPwEO2Qn/QxW1imz1X146q6qqq2V9V22vMSt1fVpN+Lsp+/439Au5onyVW0h3Lmh9jGQeunz88C7wJI8kbaQX9yqK0cvkPAv+ysvtkF/Liq/malX2Tihm5qDTcrn2R99vszwCuBr3Tmnp+tqttH1ug16rPPjdNnvx8DbkvyJHAe+E9VNbH/a+2zz/8B+G9J/j3tidkPTnoBl+SLtD+wr+rMPXwSuAKgqj5Pey7iPcBR4Azwr1b1fSb8z0mStIxJHLqRJK2AQS9JDWfQS1LDGfSS1HAGvSQ1nEEvSQ1n0EtSwxn0Uh+S3J1krrMn+P8adXuklfCCKWkZSV4FPAG8parOJnlNwzcPU8NY0UvLOw+8AvhskpYhr0lj0EvL6NzM5c3AnwIHkvzKiJskrcjEbWomDVuSnVX1Q+CRJDfQ3jVRmhiO0UvLSPJbwM8B/xc4AvxyVTXltoWaAga9JDWcY/SS1HAGvSQ1nEEvSQ1n0EtSwxn0ktRwBr0kNZxBL0kN9/8B/DZzatFEoGMAAAAASUVORK5CYII=\n"
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#5.2\n",
    "\n",
    "#投资收益问题的第3种求解\n",
    "#s从0到1以0.01步长递进\n",
    "#模型三中是通过max（最大化收益-最小化风险）来实现最终的需求，之后化为min标准化模型\n",
    "#linprog\n",
    "from scipy.optimize import *\n",
    "import matplotlib.pyplot as plt\n",
    "A=[[0,0.025,0,0,0,-1],[0,0,0.15,0,0,-1],[0,0,0,0.55,0,-1],[0,0,0,0,0.026,-1]]\n",
    "b=[0,0,0,0]\n",
    "Aeq=[[1,1.01,1.02,1.045,1.065,0]]\n",
    "beq=[1]\n",
    "bound=((0,None),(0,None),(0,None),(0,None),(0,None),(0,None))\n",
    "s=0;ss=[];aa=[]\n",
    "while s<=1:\n",
    "    c=[-(1-s)*0.05,-(1-s)*0.27,-(1-s)*0.19,-(1-s)*0.185,-(1-s)*0.185,s]\n",
    "    res=linprog(c,A,b,Aeq,beq)\n",
    "    ss.append(s);aa.append(-res.fun)# 此处求最大化收益-最小化风险\n",
    "    s=s+0.01\n",
    "plt.plot(ss,aa,'.')\n",
    "plt.xlabel('$s$')\n",
    "plt.show()\n",
    "#从图中可见，整体的收益率呈现直线下降的趋势，s是极小化风险的系数，（1-s）最大化投资收益的系数\n",
    "#表明风险系数越高，整体的收益越少"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最小风险: 0.012478538955620911\n",
      "对各家公司的投资比例 [0.38995427 0.12478533 0.03747303 0.09982825 0.19197745 0.15598168\n",
      " 0.01247854]\n"
     ]
    }
   ],
   "source": [
    "#5.3\n",
    "\n",
    "'''\n",
    "协方差矩阵的对角线，即是购买该公司股票可能造成的损失率\n",
    "固定盈利水平，求风险系数\n",
    "x[i]表示对第i家公司进行投资比例，i= 0,2,3...5\n",
    "    目标函数:将最小化风险即min{max (q[i]*x[i])}\\\n",
    "         设定x[n+1] = max(q[i]*x[i])\n",
    "    约束条件：q[i]*x[i]<=x[n+1]\n",
    "            投资和 = 1\n",
    "            收益 >= 0.25\n",
    "'''\n",
    "#linprog\n",
    "from scipy.optimize import *\n",
    "c=[0,0,0,0,0,0,1]\n",
    "A=[[-20,-42,-100,-50,-46,-30,0],[0.032,0,0,0,0,0,-1],[0,0.1,0,0,0,0,-1],\n",
    "[0,0,0.333,0,0,0,-1],[0,0,0,0.125,0,0,-1],\n",
    "[0,0,0,0,0.065,0,-1],[0,0,0,0,0,0.08,-1]]\n",
    "b=[25,0,0,0,0,0,0]\n",
    "Aeq=[[1,1,1,1,1,1,0]]\n",
    "beq=[1]\n",
    "bound=((0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,None))\n",
    "res=linprog(c,A,b,Aeq,beq,bound)\n",
    "print(\"最小风险:\",res.fun)\n",
    "print('对各家公司的投资比例',res.x)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "各个牌号的用量为： [ 580.   1420.      0.    305.7  2194.3     0.     80.97 1119.03    0.  ]\n",
      "应生产糖果\n",
      "甲:967千克\n",
      "乙:4733千克\n",
      "丙:0千克\n"
     ]
    }
   ],
   "source": [
    "#5.4\n",
    "\n",
    "#和5.3中协方差矩阵表格有点相似，都是1对多形式，生产甲乙丙都需要ABC三种原料，所以3*3需要9个参数\n",
    "#生产每一类糖果的量，都是相应ABC原料的和\n",
    "#linprog\n",
    "from scipy.optimize import *\n",
    "import numpy as np\n",
    "c=[-0.9,-0.45,0.05,-1.4,-0.95,-0.45,-1.9,-1.45,-0.95]\n",
    "A=[[-0.4,0,0,0.6,0,0,0.6,0,0],[0,-0.7,0,0,0.3,0,0,0.3,0],[-0.2,0,0,-0.2,0,0,0.8,0,0],[0,-0.5,0,0,-0.5,0,0,0.5,0],\n",
    "   [0,0,-0.6,0,0,-0.6,0,0,0.4],[1,1,1,0,0,0,0,0,0],[0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,1,1,1]]\n",
    "b=[0,0,0,0,0,2000,2500,1200]\n",
    "bound=((0,2000),(0,2000),(0,2000),(0,2500),(0,2500),(0,2500),(0,1200),(0,1200),(0,1200))\n",
    "res=linprog(c,A,b,None,None,bound)\n",
    "print('各个牌号的用量为：',np.round(res.x,2))\n",
    "a=np.array(np.reshape(res.x,(3,3)))\n",
    "print(\"应生产糖果\\n甲:%.0f千克\"%(a[0,0]+a[1,0]+a[2,0]))\n",
    "print(\"乙:%.0f千克\"%(a[0,1]+a[1,1]+a[2,1]))\n",
    "print(\"丙:%.0f千克\"%(a[0,2]+a[1,2]+a[2,2]))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "目标函数的最小解: 30.0000000244137\n",
      "最优解为: [25.613  4.387 21.58   8.42 ]\n"
     ]
    }
   ],
   "source": [
    "#5.5\n",
    "\n",
    "'''\n",
    "多目标规划问题\n",
    "#参考CSDN文章：https://blog.csdn.net/fxy11/article/details/118303557\n",
    "csdn文章中约束矩阵写错，这边已经更正过来\n",
    "把max放进约束条件,不妨假定小于等于10000\n",
    "'''\n",
    "#linprog\n",
    "from scipy.optimize import linprog\n",
    "c=[0,3,0,2]\n",
    "A=[[-1,-1,0,0],[0,0,-1,-1],[3,0,2,0],[0,3,0,2],[100,90,80,70]]\n",
    "b=[[-30],[-30],[120],[48],[10000]]\n",
    "LB=[0]*len(c)\n",
    "UB=[None]*len(c)\n",
    "bound=tuple(zip(LB,UB))\n",
    "res=linprog(c,A,b,None,None,bound)\n",
    "print(\"目标函数的最小解:\",res.fun)\n",
    "print(\"最优解为:\",np.round(res.x,3))\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}